package com.shanling.shanlingcontroller.ui.fragment;

import android.Manifest;
import android.content.ContentUris;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.chipsguide.lib.bluetooth.managers.BluetoothDeviceManager;
import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseLazyFragment;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.music.player.PlayHistoryLoader;
import com.shanling.shanlingcontroller.music.player.PlaylistLoader;
import com.shanling.shanlingcontroller.ui.activity.DeviceMusicActivity;
import com.shanling.shanlingcontroller.ui.activity.HistoryorCollectionMusicActivity;
import com.shanling.shanlingcontroller.ui.activity.LocalmusicaActivity;
import com.shanling.shanlingcontroller.utils.PermissionsUtils;
import com.shanling.shanlingcontroller.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ch1 on 2019/10/23.
 */

public class MusicFragment extends BaseLazyFragment implements PermissionsUtils.onGetpermissons {
    @BindView(R.id.rl_loacal)
    RelativeLayout rlLoacal;
    @BindView(R.id.rl_device)
    RelativeLayout rlDevice;
    @BindView(R.id.rl_recently)
    RelativeLayout rlRecently;
    @BindView(R.id.rl_collect)
    RelativeLayout rlCollect;
    private int id;

    @Override
    protected void onLazyLoad() {

    }
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_music;
    }
    @Override
    protected void initViewsAndEvents() {
        PermissionsUtils.setPermissons(this);
        if(preferenceUtil.getBleType()==2){
            rlDevice.setVisibility(View.VISIBLE);
        }else {
            rlDevice.setVisibility(View.GONE);
        }
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }
    @OnClick({R.id.rl_loacal, R.id.rl_device, R.id.rl_recently, R.id.rl_collect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_loacal:
                getPermisson(R.id.rl_loacal);
                break;
            case R.id.rl_device:
                getPermisson(R.id.rl_device);
                break;
            case R.id.rl_recently:
                getPermisson(R.id.rl_recently);
                break;
            case R.id.rl_collect:
                getPermisson(R.id.rl_collect);
                break;
        }
    }

    private void getPermisson(int viewId){
        id=viewId;
        PermissionsUtils.getPermisson(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE);
    }

    @Override
    public void onGetPermissonSuccess() {
        switch (id){
            case R.id.rl_loacal:
                readyGo(LocalmusicaActivity.class);
                break;
            case R.id.rl_device:
                int currentMode = CustomApplication.getInstance().getBluetoothDeviceManager().getCurrentMode();
                Log.e("weiyu","模式"+currentMode);
                if (CustomApplication.getInstance().getBluetoothDeviceManager().getCurrentMode() == BluetoothDeviceManager.Mode.CARD){

                }
                readyGo(DeviceMusicActivity.class);
                break;
            case R.id.rl_recently:
                readyGoHistoryorCollectionMusic(1);
                break;
            case R.id.rl_collect:
                readyGoHistoryorCollectionMusic(2);
                break;
        }
    }

    private  void readyGoHistoryorCollectionMusic(int type){
        Bundle bundle =new Bundle();
        bundle.putInt("type",type);
        readyGo(HistoryorCollectionMusicActivity.class,bundle);
    }
    @Override
    public void onGetPermissonFailed() {
        ToastUtils.showToast(getContext(),R.string.open_permission);
    }
}
